package com.example.wangyongdong.bearsqlitedemo.db;

import com.example.wangyongdong.bearsqlitedemo.entity.MoneyEntity;
import com.example.wangyongdong.bearsqlitedemo.entity.RecordEntity;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wangyongdong on 2017/4/20.
 */

public class ObserverRocords {

    public static void addRecords(RecordEntity recordEntity){
        Observable.just(recordEntity).map(new Function<RecordEntity, String>() {
            @Override
            public String apply(@NonNull RecordEntity recordEntity) throws Exception {
                DatabaseTool.getInstance().addRecord(recordEntity);
                return null;
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    public static void deleteRecord(RecordEntity recordEntity){
        Observable.just(recordEntity).map(new Function<RecordEntity, String>() {
            @Override
            public String apply(@NonNull RecordEntity recordEntity) throws Exception {
                DatabaseTool.getInstance().removeRecord(5);
                return null;
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    public static void updateRecord(RecordEntity recordEntity){
        Observable.just(recordEntity)
                .map(new Function<RecordEntity, String>() {
                    @Override
                    public String apply(@NonNull RecordEntity recordEntity) throws Exception {
                        DatabaseTool.getInstance().updateRecord(recordEntity);
                        return null;
                    }
                }).subscribeOn(Schedulers.io()).subscribe();
    }

    public static Observable<List<RecordEntity>> queryYearRecord(){
        return Observable.just("")
                .map(new Function<String, List<RecordEntity>>() {
                    @Override
                    public List<RecordEntity> apply(@NonNull String s) throws Exception {
                        return DatabaseTool.getInstance().queryCarYearRecord();
                    }
                });
    }

    public static Observable<List<MoneyEntity>> queryMonthMoney(){
        return Observable.just("")
                .map(new Function<String, List<MoneyEntity>>() {
                    @Override
                    public List<MoneyEntity> apply(@NonNull String s) throws Exception {
                        return DatabaseTool.getInstance().queryEachMonthMoney();
                    }
                });
    }

}
